<template>
  <div class="container">
    <div class="top">
      <el-button type="primary" size="mini" @click="onAdd">新增</el-button>
    </div>
    <div class="content-list">
      <el-table
        ref="table"
        :data="resultList"
        :header-cell-style="{'text-align':'center'}"
        :cell-style="{'text-align':'center'}"
        borderstyle="width: 100%"
      >
        <el-table-column prop="name" label="成果名称" />
        <el-table-column prop="createTime" label="发布时间" />
        <el-table-column label="操作">
          <template slot-scope="{row}">
            <el-button size="small" type="text" @click="onEdit(row.id)">编辑</el-button>
            <el-button size="small" type="text" @click="onDelete(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="pageList">
      <div class="page">
        <paging ref="paging_zi" @CurrentPage="accept_CurrentValue" />
      </div>
    </div>
    <el-dialog :visible="visible" @close="close">
      <div class="add-content">
        <el-form ref="resultFrom" :rules="formRules" :model="resultFrom" label-width="80px">
          <el-form-item prop="contacts" label="联系人" required>
            <el-input v-model="resultFrom.contacts" style="width:300px"></el-input>
          </el-form-item>
          <el-form-item prop="phoneNumber" label="联系电话" required>
            <el-input v-model="resultFrom.phoneNumber" style="width:300px"></el-input>
          </el-form-item>
          <el-form-item prop="name" label="需求名称" required>
            <el-input v-model="resultFrom.name" style="width:300px"></el-input>
          </el-form-item>
          <el-form-item prop="introduce" label="需求介绍" required>
            <el-input
              type="textarea"
              :autosize="{ minRows: 4, maxRows: 20}"
              v-model="resultFrom.introduce"
              style="width:300px"
            ></el-input>
          </el-form-item>
          <el-form-item prop="region" label="地区" required>
            <el-cascader
              ref="area"
              placeholder="请选择地区"
              v-model="resultFrom.region"
              style="width:200px"
              :options="areaList"
              :props="{ label:'value', value:'value',checkStrictly: true}"
            />
          </el-form-item>
        </el-form>
      </div>
      <div class="footer flex flex-end">
        <el-button class="btn-plain" size="small" @click="close">取 消</el-button>
        <el-button type="primary" size="small" @click="onSave">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { Loading } from "element-ui";
import {
  imgBaseUrl,
  resultAreaInfo,
  demandDetail,
  demandAdd,
  demandUpdate,
  demandDelete,
  myScienceAchievementsDemandList
} from "../../../api/api.js";
import paging from "@/components/paging.vue";
export default {
  components: { paging },
  data() {
    return {
      type: 1, // 1新增 2编辑
      visible: false,
      imgBaseUrl: imgBaseUrl,
      resultList: [],
      areaList: [],
      uploadUrl: "https://bootapi.51bmj.cn/api/home/upload",
      resultFrom: {
        contacts: "",
        phoneNumber: "",
        name: "",
        introduce: "",
        region: []
      },
      formRules: {
        contacts: [
          {
            required: true,
            message: "请输入联系人",
            trigger: "blur"
          }
        ],
        phoneNumber: [
          {
            required: true,
            message: "请输入联系电话",
            trigger: "blur"
          },
          { pattern: /^1[0-9]{10}$/, message: "请输入正确的11位手机号码" }
        ],
        name: [
          {
            required: true,
            message: "请输入需求名称",
            trigger: "blur"
          }
        ],
        introduce: [
          {
            required: true,
            message: "请输入需求介绍",
            trigger: "blur"
          }
        ],
        province: [
          {
            required: true,
            message: "请选择地区",
            trigger: "change"
          }
        ]
      },
      form: {
        pageNo: 1,
        pageSize: 10
      }
    };
  },
  mounted() {
    this.getList();
    this.getAreaList();
  },
  methods: {
    // 列表数据
    getList() {
      myScienceAchievementsDemandList(this.form).then(res => {
        this.resultList = res.result.records;
        this.$refs.paging_zi.totalValue(res.result.total);
      });
    },
    accept_CurrentValue(value) {
      this.form.pageNo = value;
      this.getList();
    },
    // 获取地区
    getAreaList() {
      resultAreaInfo().then(res => {
        this.areaList = res.result;
      });
    },
    onEdit(id) {
      this.type = 2;
      demandDetail({ id: id }).then(res => {
        this.resultFrom = res.result;
      });
      this.visible = true;
    },
    onDelete(id) {
      this.$confirm(`确认删除?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        demandDelete({ id: id }).then(res => {
          this.getList();
          this.$message({ type: "success", message: "操作成功!" });
        });
      });
    },
    onAdd() {
      this.type = 1;
      this.visible = true;
    },
    onSave() {
      this.$refs["resultFrom"].validate(async (valid, item) => {
        if (valid) {
          if (this.type === 1) {
            demandAdd(this.resultFrom).then(res => {
              if (res.code === 200) {
                this.$message({ type: "success", message: "操作成功!" });
                this.getList();
                this.close();
              } else {
                this.$message({ type: "warning", message: res.message });
              }
            });
          } else {
            demandUpdate(this.resultFrom).then(res => {
              if (res.code === 200) {
                this.$message({ type: "success", message: "操作成功" });
                this.getList();
                this.close();
              } else {
                this.$message({ type: "warning", message: res.message });
              }
            });
          }
        }
      });
    },
    close() {
      this.visible = false;
      this.resultFrom.contacts = "";
      this.resultFrom.phoneNumber = "";
      this.resultFrom.name = "";
      this.resultFrom.introduce = "";
      this.resultFrom.region = [];
    }
  }
};
</script>
<style lang="less" scoped>
.add-content {
  .img-content {
    .img-box {
      position: relative;
      width: 150px;
      height: 150px;
      display: inline-block;
      margin: 0 10px 10px 0;
    }
    img {
      width: 150px;
      height: 150px;
      z-index: 9;
    }
    .mask {
      width: 150px;
      height: 150px;
      background: rgba(0, 0, 0, 0.4);
      position: absolute;
      top: 0;
      font-size: 18px;
      opacity: 0;
      z-index: 99;
    }
    .mask:hover {
      opacity: 1;
    }
  }
  .upload-img {
    border: 1px solid #d9d9d9;
    width: 150px;
    height: 150px;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 150px;
    height: 150px;
    line-height: 150px;
    text-align: center;
  }
}
</style>